package cn.tedu._03vehicle.base.filters;

import cn.tedu._03vehicle.pojo.vo.UserVO;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.session.StandardSession;

import java.io.IOException;
@Slf4j
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = {"/v1/vehicle/*"})
public class LoginCheckFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("过滤器初始化....");
    }

   //doFilter()过滤器的核心方法
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.debug("进入登录状态校验的过滤器.......");
        //1.类型转换
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取会话对象
        HttpSession session = request.getSession();
        //获取会话标识
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO != null) {//登录状态正常
            //有此行代码 表示放行 无此行代码 表示拦截
            filterChain.doFilter(request, response);
        } else {

        }
    }
    @Override
    public void destroy() {
        log.debug("过滤器销毁....");
    }

}
